using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CrossFlow.GraphLib;

namespace CrossFlowContract
{
  [TestClass]
  public class SimpleGraphs
  {
    [TestMethod]
    public void graph0()
    {
      DirectedGraph graph1 = new DirectedGraph("graph1");
      Assert.AreEqual("graph1", graph1.Name);
      Assert.IsNotNull(graph1.GUID);
      Vertex r = new Vertex("1");
      Vertex v = new Vertex("2");
      Edge e = new Edge(r, v);
      graph1.AddNodes(r, v, e);
      Assert.IsTrue(graph1.NodeCount == 3);
      Assert.IsTrue(graph1.VertexCount == 2);
      Assert.IsTrue(graph1.EdgeCount == 1);
      Assert.AreSame(e.Source, r);
      Assert.AreSame(e.Target, v);
    }
    [TestMethod]
    public void toBytes()
    {
      DirectedGraph graph1 = new DirectedGraph("graph1");
      Assert.AreEqual("graph1", graph1.Name);
      Assert.IsNotNull(graph1.GUID);
      Vertex r = new Vertex("1");
      Vertex v = new Vertex("2");
      Edge e = new Edge(r, v);
      graph1.AddNodes(r, v, e);

      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serial = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
      System.IO.Stream xmlbytes = new System.IO.MemoryStream(1024);
      serial.Serialize(xmlbytes, graph1);

      xmlbytes.Position = 0;
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserial = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
      DirectedGraph graph2 = deserial.Deserialize(xmlbytes) as DirectedGraph;

      Assert.IsTrue(graph2.NodeCount == 3);
      Assert.IsTrue(graph2.VertexCount == 2);
      Assert.IsTrue(graph2.EdgeCount == 1);
      IEnumerator<Edge> iterator = graph2.InitialVertex.Edges.GetEnumerator();
      Assert.IsTrue(iterator.MoveNext());
      Assert.AreSame(graph2.InitialVertex, iterator.Current.Source);
    }
  }
}